package mall.kotlin.com.myapplication.android6_0.permission;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import mall.kotlin.com.myapplication.R;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

//必须添加的注释  用来注册一个Activity或Fragment ,使他们可以处理权限
@RuntimePermissions
public class PermissDisPatcherActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permiss_dis_patcher);
        Button call = findViewById(R.id.bt_call);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissDisPatcherActivityPermissionsDispatcher.callWithCheck(PermissDisPatcherActivity.this);
            }
        });
    }

    //必须添加的注释,在需要获取权限的地方获取注释,用来获取权限
    @NeedsPermission(Manifest.permission.CALL_PHONE)
    void call() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //提示用户为何要开启此权限,在用户选择拒绝后,再次需要访问该权限是调用
    @OnShowRationale(Manifest.permission.CALL_PHONE) //多个{Manifest.permission.CALL_PHONE,Manifest.permission.CALL_PHONE}
    void showWhy(final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setMessage("提示用户为何要开启此权限")
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed(); //再次执行权限请求
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.cancel();//取消执行请求
                    }
                })
                .show();

    }

    //当用户拒绝时调用
    @OnPermissionDenied(Manifest.permission.CALL_PHONE)
    void showDenied() {
        Toast.makeText(this, "用户选择拒绝时的提醒", Toast.LENGTH_SHORT).show();
    }

    //用户选择不再询问是调用
    @OnNeverAskAgain(Manifest.permission.CALL_PHONE)
    void showNotAsk() {
        new AlertDialog.Builder(this)
                .setMessage("该功能需要访问电话的权限,不开启将无法进行")
                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(PermissDisPatcherActivity.this, "用户选择不再询问是调用", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissDisPatcherActivityPermissionsDispatcher.onRequestPermissionsResult( PermissDisPatcherActivity.this,requestCode,grantResults);
    }
}
